package com.zjdiepu.www.biz.dh;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.dh.DhOverdueLawsuitMapper;
import com.zjdiepu.www.ibiz.dh.IDhOverdueLawsuitService;
import com.zjdiepu.www.model.dh.dto.DhOverdueLawsuit;
import com.zjdiepu.www.model.dh.ext.DhOverdueLawsuitExt;

/**
 * Service层操作实现<br/>
 * @author zhangzhengjian<br/>
 * @time 2017年06月14日 10时13分<br/>
 * @since 1.0<br/>
 */
@Service
public class DhOverdueLawsuitServiceImpl extends BaseService implements IDhOverdueLawsuitService {
	public static final Logger logger = LoggerFactory.getLogger(DhOverdueLawsuitServiceImpl.class);

	@Autowired
	private DhOverdueLawsuitMapper dhOverdueLawsuitMapper;
	
	@Override
	public void saveUpdateAndDelete(DhOverdueLawsuit dhOverdueLawsuit, OperateType operType) {
		switch (operType) {
		case add:
			dhOverdueLawsuit.initInsertData(dhOverdueLawsuit.getModifier(), dhOverdueLawsuit.getModifierName());
			dhOverdueLawsuitMapper.insert(dhOverdueLawsuit);
			break;
		case edit:
			dhOverdueLawsuit.initUpdateData(dhOverdueLawsuit.getModifier(), dhOverdueLawsuit.getModifierName());
			// 影响的记录数
			int updateCount = dhOverdueLawsuitMapper.updateByPrimaryKeySelective(dhOverdueLawsuit);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			dhOverdueLawsuit.initDeleteData(dhOverdueLawsuit.getModifier(), dhOverdueLawsuit.getModifierName());
			// 影响的记录数
			int deleteCount = dhOverdueLawsuitMapper.updateByPrimaryKeySelective(dhOverdueLawsuit);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<DhOverdueLawsuit> selectByExt(DhOverdueLawsuitExt dhOverdueLawsuitExt) {
		return dhOverdueLawsuitMapper.selectByExt(dhOverdueLawsuitExt);
	}
	
	@Override
	public int countByExt(DhOverdueLawsuitExt dhOverdueLawsuitExt){
		return dhOverdueLawsuitMapper.countByExt(dhOverdueLawsuitExt);
	}
	
	@Override
	public DhOverdueLawsuit selectById(String id) {
		return dhOverdueLawsuitMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			dhOverdueLawsuitMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(DhOverdueLawsuit dhOverdueLawsuit) {
		return dhOverdueLawsuitMapper.countRefined(dhOverdueLawsuit);
	}

	@Override
	public DhOverdueLawsuit selectRefined(DhOverdueLawsuit dhOverdueLawsuit) {
		return dhOverdueLawsuitMapper.selectRefined(dhOverdueLawsuit);
	}

	@Override
	public boolean repeat(DhOverdueLawsuit dhOverdueLawsuit) {
		int count = countRefined(dhOverdueLawsuit);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(dhOverdueLawsuit.getId())) {
			return true;
		}
		DhOverdueLawsuit dbItem = selectRefined(dhOverdueLawsuit);
		if(null == dbItem) {
			return false;
		}
		return !dhOverdueLawsuit.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return dhOverdueLawsuitMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
}
